package com.epam.training.generics.task2;

import java.util.*;

/**
 * 
 * @author bogdan
 *
 */
public class FruitCopy {

	private FruitCopy() {
	}
	
	/**
	 * Main test method
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		List<Fruit> fr = new LinkedList<Fruit>();
		fr.add(new Fruit());
		List<Apple> app = new LinkedList<Apple>();
		app.add(new Apple());
		List<Orange> or = new LinkedList<Orange>();
		or.add(new Orange());
		List<RedApple> rapp = new LinkedList<RedApple>();
		rapp.add(new RedApple());
		List<Melon> mel = new LinkedList<Melon>();
		mel.add(new Melon());

		copyAll(or, fr);
		copyAll(rapp, fr);
		copyAll(rapp, app);
		copyAll(rapp, rapp);
		copyAll(rapp, rapp);
		// copyAll(fr, or);
	}

	/**
	 * Copy all from one to another
	 * 
	 * @param colection
	 * @param copied
	 */
	public static <T> void copyAll(List<T> colection, List<? super T> copied) {
		for (int i = 0; i < colection.size(); i++) {
			copied.add((T) colection.remove(i));
		}

	}

	/**
	 * Copy item then over variable compare
	 * 
	 * @param item
	 * @param to
	 * @param compare
	 * @param comparator
	 */
	public static <T> void copyOverItem(List<T> item, List<T> to, T compare,
			Comparator<T> comparator) {
		for (int i = 0; i < item.size(); i++) {
			final T tmp = item.get(i);
			if (comparator.compare(tmp, compare) > 0) {
				to.add(tmp);
			}
		}
	}
}
